//
//  OrderXmlParser.m
//  OrderOnline
//
//  Created by YAPPA_3 on 11/30/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "OrderXmlParser.h"

static NSString* gTypeNames[eXmlParseTypeNum] =
{
	@"menuFood",
	
};

@interface OrderXmlParser(private)
#define PARSE_START_F(x)		- (void)parseStart##x:(NSString*)elementName attributes:(NSDictionary*)attributeDict
#define PARSE_END_F(x)			- (void)parseEnd##x:(NSString*)elementName
#define PARSE_CHARACTERS_F(x)	- (void)parseCharacters##x:(NSString*)string
#define PARSE_F(x)			PARSE_START_F(x);PARSE_END_F(x);PARSE_CHARACTERS_F(x);

PARSE_F( menuFood );


#define DEL_F(x)		- (void)delete##x
#define C_DEL_F(x)		[self delete##x];

DEL_F( menuFood );

@end



@implementation OrderXmlParser
@synthesize m_menuFood;



- (id)init
{
	if( self = [super init] )
	{
		[m_FuncTbl removeAllObjects];
		for( int i=0; i<eXmlParseTypeNum; i++ )
		{
			ParserFuncTbl* tbl = [[[ParserFuncTbl alloc] init] autorelease];
			tbl.start = [NSString stringWithFormat:@"parseStart%@:attributes:", gTypeNames[i]];
			tbl.end = [NSString stringWithFormat:@"parseEnd%@:", gTypeNames[i]];
			tbl.characters = [NSString stringWithFormat:@"parseCharacters%@:", gTypeNames[i]];
			[m_FuncTbl addObject:tbl];
		}
		m_menuFood = [[MenuFood alloc] init];
		m_currentStr      = [[NSMutableString alloc] init];
	}
	return self;
}
- (BOOL)parseXml:(NSData*)data funcNo:(u_int)func_no
{
	BOOL ret = NO;
	
	switch( func_no )
	{
		case eXmlParseMenuFood:
		{
			C_DEL_F( menuFood )
			break;
		}
		
		default: break;
	}
	ret = [super parseXml:data funcNo:func_no]; 
	return ret;
}

#pragma mark -
#pragma mark Delete functions

DEL_F( menuFood )
{
	SAFE_RELEASE(m_menuFood);
	m_menuFood = [[MenuFood alloc] init];
	SAFE_RELEASE(m_currentStr)
	m_currentStr      = [[NSMutableString alloc] init];
}


- (void)deleteData:(eXmlParseType)type
{
	switch( type )
	{
		case eXmlParseMenuFood:
		{
			C_DEL_F( menuFood );
			break;
		}
		default: break;
	}
}

#pragma mark -
#pragma mark Parse functions

//--------------------------------------------------------------------
//- (void)parsemenuFood:(NSString*)elementName attributes:(NSDictionary*)attributeDict
PARSE_START_F( menuFood )
{
	[m_currentStr setString: TEXT_SPACE];
	if ([elementName isEqualToString:@"food"]) {
		m_food = [[Food alloc] init];
		m_food.m_productID = [attributeDict objectForKey:@"id"] ? : TEXT_SPACE;
		m_food.m_type = [attributeDict objectForKey:@"type"] ? : TEXT_SPACE;
	}
	
}
//- (void)parseEndmenuFood:(NSString*)elementName
PARSE_END_F( menuFood ){

	if ([elementName isEqualToString:@"image"])
	{
		[m_food.m_linkImage setString:m_currentStr];
	}
	if ([elementName isEqualToString:@"title"])
	{
		[m_food.m_title setString:m_currentStr];
	}
	if ([elementName isEqualToString:@"discount"])
	{
		[m_food.m_discount setString:m_currentStr];
	}
	if ([elementName isEqualToString:@"price"])
	{
		m_food.m_price = [m_currentStr floatValue];
		
	}
	if ([elementName isEqualToString:@"pricediscount"])
	{
		m_food.m_priceDiscount = [m_currentStr floatValue];
	}
	if ([elementName isEqualToString:@"food"])
	{
		[m_menuFood.m_listFood addObject:m_food];
		[m_food release];
	}
	
}
//- (void)parseCharactersmenuFood:(NSString*)string
PARSE_CHARACTERS_F( menuFood ){
	[m_currentStr appendString:string];
}

- (void)dealloc
{
	SAFE_RELEASE(m_menuFood)
	SAFE_RELEASE(m_currentStr)
	[super dealloc];
}


@end
